package com.cn.action;

import com.cn.entity.NewsEntity;
import com.cn.service.NewsService;
import com.cn.utilOne.PageBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class NewsShowAction implements ActionInterface {
    NewsService ns = new NewsService();

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        NewsEntity newsEntity = new NewsEntity();
        String newsid = request.getParameter("newsid");
        String op = request.getParameter("op");
        if (op != null && op.equals("newsshow")) {
            this.query(request, response);
            return "ht_news_queryAll.jsp";
        } else if (op != null && op.equals("oneNewsShow")) {
            return null;
        } else {
            newsEntity.setNews_id(newsid);
            NewsEntity newsEntity1 = ns.queryById(newsEntity);
            request.setAttribute("newsshow", newsEntity1);
            this.query(request, response);
            return "udai_notice.jsp";
        }

    }

    public String query(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        /*
         * 处理GET请求方式编码问题！
         */
        NewsEntity newsEntity = new NewsEntity();
        int pc = this.getPc(request);
        int ps = 5;
        PageBean<NewsEntity> pageBean = (PageBean<NewsEntity>) ns.findByCriteria(null, pc);//传递pc, ps给Service，得到PageBean
        pageBean.setUrl(getUrl(request));
        request.getSession().setAttribute("pageBean", pageBean.getBeanList());
        request.setAttribute("pb", pageBean);
        return "udai_notice.jsp";
    }

    /**
     * 得到当前页码：pc
     * 如果pc参数不存在，默认pc=1
     * 如果pc参数存在，需要转换成int类型即可
     */
    private int getPc(HttpServletRequest request) {
        String value = request.getParameter("pc");
        if (value == null || value.trim().isEmpty()) {
            return 1;
        }
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return 1;
        }

    }


    private String getUrl(HttpServletRequest request) {
        String contextPath = request.getContextPath();//获取项目名
        String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
        String queryString = request.getQueryString();//获取问号之后的参数部份
        System.out.println(contextPath + servletPath + "?" + queryString);
        //  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
        if (queryString.contains("&pc=")) {
            int index = queryString.lastIndexOf("&pc=");
            queryString = queryString.substring(0, index);
        }
        return contextPath + servletPath + "?" + queryString;
    }
}
